<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order extends CI_Controller {
	
	
	public $user_data=array();
	public $WEB_SET;
	
	function __construct()
     {
	     parent::__construct();
		
		//按当前语言载入语言文件     	
     	if($this->session->userdata("lang")=="")
     	{
			$this->lang->load('info',$this->config->item("language")); 
		}else
		{
			$this->lang->load('info',$this->session->userdata("lang")); 
		}
		if (!$this->session->userdata('user_in')){
			
			$urldata="";
			foreach($_GET as $sname => $svalue)
			{
				$urldata.=$sname."=".$svalue."&";
			}
			//有POST数据的话房SESSION里
			if($_POST)
			{
				$this->session->set_userdata(array("page_LastPost" => $_POST));
			}
			$urldata=uri_string().$urldata;
			$this->session->set_userdata(array("page_returnurl" => $urldata));
			
			redirect('user/login?returnurl='.urlencode($urldata));
			exit();
		}
		
		//加载网站设置
		$this->load->model("Agent_model");
		$this->WEB_SET=$this->Agent_model->load_websetinfo();
		
		$this->user_data["user_account"] = $this->session->userdata('user_account');
		$this->user_data["user_name"] = $this->session->userdata('user_name');
		
		$this->user_data["AgentCode"] = $this->session->userdata('WEB_AgentCode');
		$this->user_data["AgentName"] = $this->session->userdata('WEB_AgentName');
		$this->user_data["mainCurrency"] = $this->session->userdata('WEB_mainCurrency');
		$this->user_data["isrebate"] 	= $this->session->userdata('isrebate');
		
		//加载模型层
		$this->load->model('Order_model');
		
		//记录日志
		$this->load->model("Userlog_model");
		$this->Userlog_model->write_userlog();

     }
    
	//订单预订
	public function bookflight()
	{
		$this->load->library('encrypt');
		$data = array();
	   	$data['css'][0] = '<link rel="stylesheet" type="text/css" href="'.site_url('css/validator.css').'" />';
	   	$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")].'-book';
	   	
	   	//设置左边选中框的默认值
	   	$data['sDepartDate']	=$this->input->post("DepartDate");
	   	$data['sReturnDate']	=$this->input->post("ReturnDate");
	   	
		$data["isback"]		=$this->input->post("isback");	//是否回程
		$data["goFlightData"]	=$this->input->post("goflightdata");	//去程航班信息
		
	   	if($this->input->post("go_flightinfo")=="")
	   	{
	   		redirect("home");	
	   	}
	   	//设置航空公司下拉菜单
	   	$airoption =lang("aircompany");
	   	$airlist['']='==All airline==';
	   	foreach ($airoption as $airid => $airname)
	   	{
	   		$airlist[$airid]=$airid.'-'.$airname;
	   	}

	   	$data['selairline']=form_dropdown('airline', $airlist, '','style="width:164px"');
	   
	   	//设置预订信息
	   	$this->load->model('Air_model');
	   	$this->load->model('Policy_model');
	   	$data['flightdata']		=$this->input->post("go_flightinfo");
	   	$data['flightbackdata']	=$this->input->post("back_flightinfo");
	   
	   	$arairinfo=$this->Air_model->getarrbookinfo($this->encrypt->decode($data['flightdata']),$this->encrypt->decode($data['flightbackdata']));	   	
	   	
	   	//国际运价的话即时读取一次运费
	   //print_r($arairinfo);
	   	if(substr($arairinfo[0]["PFD"],0,3)=="PFI")
	   	{
	   		
	   		//替换燃油费
	   		$tmpdata=$this->encrypt->decode($data['flightdata']);
	   		//没有运价的时候算一次运价
	   		if(strpos($tmpdata,"|-1|")>-1)
	   		{
		   		$arTaxdata=array(
		   			"fromport" => $arairinfo[0]["fromport"],
		   			"destport" => $arairinfo[0]["destport"],
		   			"FlightNo" => $arairinfo[0]["FlightNo"],
		   			"flightdate" => date("Y-m-d",strtotime($arairinfo[0]["flightdate"])),
		   			"class"	=> $arairinfo[0]["class"][0][0]
		   		);
		   
		   		$arairinfo[0]["OilFee"]=$this->Air_model->ws_queryiTaxbyflight($arTaxdata);
		   		
		   		
		   		$tmpdata=str_replace("|-1|","|".$arairinfo[0]['OilFee']."|",$tmpdata);
		   		$data['flightdata']		=$this->encrypt->encode($tmpdata);
		   		
		   		$tmpdata=$this->encrypt->decode($data['flightbackdata']);
		   		//echo $tmpdata;
		   		$tmpdata=str_replace("|-1|","|".$arairinfo[0]['OilFee']."|",$tmpdata);
		   	
		   		
		   		
		   		if($data['flightbackdata']!="")
		   		{
		   			$data['flightbackdata']	=$this->encrypt->encode($tmpdata);
		   			$arairinfo[1]["OilFee"]=$arairinfo[0]["OilFee"];
		   		}
	   		}
	   		$data["queryaction"]="/home/flight/international";
	   		
	   	}else
	   	{
	   		$data["queryaction"]="/home/flight";	
	   	}
	   	
	   
	   	$data['arairinfo']=$arairinfo;
	   	
	   	$data["TGQNote"]=$this->Policy_model->get_TGQNotebyID($arairinfo[0]["PFDID"]);
	   	$data['sFromcitycode']	=$arairinfo[0]['fromport'];
	   	$data['sDestcitycode']	=$arairinfo[0]['destport'];
	   	
	   	$data["mainCurrency"]	=$this->user_data["mainCurrency"];
	   	
	   	//设置证件类型
	   	//设置航空公司下拉菜单
	   	$idType =lang("idType");

	   	$data['selCTType']=form_dropdown('CTType[]',$idType, '','id="sel_CTType_REPLACE_INDEX"');
	   	
	   	
	   	//设置默认联系方式
	   	$this->load->model("User_model");
	   	$linkinfo=$this->User_model->get_customer($this->user_data["user_account"]);
	   	
	   	$data["txLinkMan"]=$linkinfo["username"];
	   	$data["txmobile"]=$linkinfo["mobilephone"];
	   	$data["txmobile2"]=$linkinfo["tel1"];
	   	$data["txEmail"]=$linkinfo["Email"];
	   	
	   	//国际航班使用国际航班预订模板
	   	if(substr($arairinfo[0]["PFD"],0,3)=="PFI")
	   	{
	   		$data['selCTType']=form_dropdown('CTType[]',$idType, '','id="sel_CTType_REPLACE_INDEX" style="width:155px"');
	   		$this->load->view('international/bookflight',$data);
	   	}else
	   	{
	   		$this->load->view('order/bookflight',$data);	
	   	}
		
	
	}
	//创建订单
	public function createorder()
	{
		//print_r($_POST);
		
		
		$bookresult = $this->Order_model->create_airOrder($_POST,$this->user_data);
    		echo '<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>';
    		if($bookresult[0])
    		{
    			$url="airorder/$bookresult[1]";
			echo "<SCRIPT LANGUAGE=\"JavaScript\">";
			echo "alert(\"booking success. Your order number:$bookresult[1]\");";
			echo "window.location='$url';";
			echo "</SCRIPT>";  
    		}else
    		{
    			
    			echo "booking fail";
    		}
		
		
	}
	
	//订单信息显示
	public function airorder($sOrderno)
	{
		$data=array();
		
		$data['css'][0] = "";
		$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")].'-myorder';
		
				
		
		$bookdata= $this->Order_model->get_airOrder($sOrderno,$this->user_data);
		
		$this->load->model('Air_model');
		$flightdata=explode("^",$bookdata["order"]->Flightinfo1."^");
	   	$data['arairinfo']=$this->Air_model->getarrbookinfo($flightdata[0],$flightdata[1]);
	   	
	   	$data['sOrderno']=$sOrderno;
	   	$data['sFromcity']=$bookdata["order"]->fromportName;
	   	$data['sDestcity']=$bookdata["order"]->destportName;
	   	$data['sDepartDate']=date("Y-m-d",strtotime($bookdata["order"]->departdate));
	   	$data['sReturnDate']=date("Y-m-d",strtotime($bookdata["order"]->returndate));
	   	
	   	if($data['sReturnDate']=="1970-01-01")
	   	{
	   		$data['sReturnDate']="";
	   	}
		
		$data['TGQNote']=$bookdata["order"]->TGQNote;
		$data['LinkMan']=$bookdata["order"]->Linkman;
	   	$data['Mobile1']=$bookdata["order"]->LinkMobile1;
	   	$data['Mobile2']=$bookdata["order"]->LinkMobile2;
	   	$data['Email']	=$bookdata["order"]->LinkEmail;
	   	$data['mainCurrency']=$bookdata["order"]->mainCurrency;
	   	
	   	$data['cpsdata']=$bookdata["passlist"];
	   	
	   	$data['TotalPrice']=$bookdata["order"]->TotalPrice;
	   	$data['TotalPassenger']=$bookdata["order"]->TotalPassenger;
	   	
	   	$arOrderStatus=lang("OrderStatus");
	   	$data['OrderStatusDesc'] =$arOrderStatus[$bookdata["order"]->Status];
	   	$data['loglist']=$this->Order_model->getOrderlog($sOrderno,"公开日志");
	   	
	   	$data['aridType'] 	=lang("idType");
		$data['arPassType']	=lang("PassType");
		$data['arGender']	=lang("Gender");
		
		
		
		$this->load->view("order/airorder",$data);
	}
	
	//查询订单
	public function queryorder()
	{
		$data = array();
	   	$data['css'][0] ="";
	   	$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")].'-queryorder';
	   	
	   	$data['sOrderno']		=$this->input->post("OrderNo");
	   	$data['sPassengerName']	=$this->input->post("PassengerName");
	   	
	   	$data["bookdate1"]=date("Y-m-d");
	   	
	   	if ( !isset($_POST['OrderNo']))
	   	{
	   		$defaultdata=array(
	   			"OrderNo" => "",
	   			"PassengerName" => "",
	   			"bookdate1" => $data["bookdate1"],
	   			"bookdate2" => ""   		
	   		);
	   		
	   		$result= $this->Order_model->query_airOrder($defaultdata,$this->user_data["user_account"]);
	   		$data['OrderStatus'] =lang("OrderStatus");
	   		$data["myorder"]=$result;
	   	}else
	   	{	   		
	   		$result= $this->Order_model->query_airOrder($_POST,$this->user_data["user_account"]);
	   		$data['OrderStatus'] =lang("OrderStatus");
	   		$data["myorder"]=$result;
	   	}
	   	
	   	
	   	
		$this->load->view("order/queryorder",$data);	
	}
	
	//我的账号信息
	public function mystuff()
	{
		$data=array();
		
		$data['css'][0] ="";
	   	$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")].'-mystuff';
	   	
	   	$this->load->model("User_model");
	   	$aruser=$this->User_model->get_customer($this->user_data["user_account"]);
		
		$data["txuseraccount"]=$this->user_data["user_account"];
		$data["username"]	=$aruser["username"];
		$data["birthday"]	=$aruser["birthday"];
		$data["tel"]		=$aruser["tel1"];
		$data["mobilephone"]=$aruser["mobilephone"];
		$data["address"]	=$aruser["address"];
		$data["email"]		=$aruser["Email"];
		
		$this->load->view("user/mystuff",$data);
	}
	
	//修改密码
	public function modifypassword()
	{
		$data=array();
		
		$data['css'][0] ="";
	   	$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")].'-mystuff';
	   	
		
		$this->load->view("user/modifyPassword",$data);
	}
	//处理修改密码
	public function doModifypassword()
	{
		//检查原始密码
		$this->load->model("User_model");
		echo '<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>';
		$_customer = $this->User_model->check_customer($this->user_data["user_account"],$this->input->post("CurPassword"),$this->session->userdata("WEB_AgentCode"));
		
		$url=site_url("order/modifypassword");
		
		if($_customer)
		{
			$passdata=array(
				"userpassword" => md5($this->input->post("NewPassword"))
			);
			$this->User_model->modifyuserdata($this->user_data["user_account"],$passdata);
			
			echo "<SCRIPT LANGUAGE=\"JavaScript\">";
			echo "alert(\"Modify Success!\");";
			echo "window.location='$url'";
			echo "</SCRIPT>";  	
			
		}else
		{
			
			echo "<SCRIPT LANGUAGE=\"JavaScript\">";
			echo "alert(\"Incorrect original password, please retry!\");";
			echo "window.location='$url'";
			echo "</SCRIPT>";  	
		}
	}
	
	//修改客人信息
	public function doeModifyinfo()
	{
		$this->load->model("User_model");
		
		$modeifyData=array(
			"username" =>  $this->input->post("username"),
			"birthday" => Date("Y-m-d",strtotime($this->input->post("birthday"))),
			"tel1" => $this->input->post("tel"),
			"mobilephone" => $this->input->post("mobilephone"),
			"address" => $this->input->post("address"),
			"Email" => $this->input->post("email")
		);
     
     	$saveresult = $this->User_model->modifyuserdata($this->user_data["user_account"],$modeifyData);	
     	
     	$url=site_url("order/mystuff");
    		if($saveresult)
    		{
			echo "<SCRIPT LANGUAGE=\"JavaScript\">";
			echo "alert(\"save success\");";
			echo "window.location='$url';";
			echo "</SCRIPT>";  
    		}else
    		{
    			
    			echo "save fail";
    		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
